﻿Imports Microsoft.Win32

''' <summary>
''' Action for saving a project under a new filename.
''' </summary>
''' <remarks></remarks>
''' <editHistory></editHistory>
Public Class SaveAsAction

    Public Shared Function CanExecute(ByVal scriptProject As ScriptProject) As Boolean
        Return scriptProject IsNot Nothing
    End Function

    ''' <summary>
    ''' Executes the action.
    ''' </summary>
    ''' <param name="scriptProject">The script project to save</param>
    ''' <param name="filename">This is the new filename that the user selected.</param>
    ''' <returns>True if the operation was completed, false otherwise.</returns>
    Public Shared Function Execute(ByVal scriptProject As ScriptProject, ByRef filename As String) As Boolean
        If scriptProject Is Nothing Then
            Throw New ArgumentNullException("scriptProject")
        End If

        Dim dialog As New SaveFileDialog

        'Set the initial filename
        dialog.FileName = filename

        'Set the filter for the dialog
        dialog.Filter = Constants.ProjectFileFilter

        'Show the dialog
        Dim result As Boolean? = dialog.ShowDialog()

        'Did the user cancel?
        If result.HasValue AndAlso result.Value Then

            'Save the project
            scriptProject.Save(dialog.FileName)

            'Ok, we're good.
            filename = dialog.FileName

            'Return completion
            Return True

        Else
            Return False
        End If

    End Function

End Class
